package com.lzx.adx.report.util.biz.product;

import cn.hutool.core.util.URLUtil;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class KkChannelHelper {

    /**
     * 夸克的上报封装
     * @param accountDTO
     * @param reportDTO
     * @return
     */
    public static String covertUrlReport(AccountDTO accountDTO, ReportDTO reportDTO) {
        Map<String,Object> params=new HashMap<>();
        params.put("adGroupld",reportDTO.getAdgroupId());
        params.put("campaignld",reportDTO.getCampaignId());
        params.put("creativeld",reportDTO.getContentId());

//        params.put("oaid", StringUtils.isNotBlank(aqyRequest.getOaid())?aqyRequest.getOaid():"");
//        params.put("oaidSum","");
//        if(StringUtils.isNotBlank(aqyRequest.getImei()) && !aqyRequest.getImei().equals("__IMEI__")) {
//            params.put("imeiSum", aqyRequest.getImei());
//        }else{
//            params.put("imeiSum","");
//        }
//        params.put("imei","");


        if (StringUtils.isNotBlank(reportDTO.getOaid()) && !reportDTO.getOaid().contains("__")) {
            params.put("oaid", reportDTO.getOaid());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaidSum()) && !reportDTO.getOaidSum().contains("__")) {
            params.put("oaidmd5", reportDTO.getOaidSum());
        }

        if(StringUtils.isNotBlank(reportDTO.getImei()) && !reportDTO.getImei().contains("__")) {
            params.put("imei", reportDTO.getImei());
        }
        if(StringUtils.isNotBlank(reportDTO.getImeiSum()) && !reportDTO.getImeiSum().contains("__")) {
            params.put("imeimd5", reportDTO.getImeiSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getIdfa()) && !reportDTO.getIdfa().contains("__")){
            params.put("idfa", reportDTO.getIdfa());
        }
        if(StringUtils.isNotBlank(reportDTO.getIdfaSum()) && !reportDTO.getIdfaSum().contains("__")){
            params.put("idfaMd5", reportDTO.getIdfaSum());
        }


        params.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent())? URLUtil.encode(reportDTO.getUser_agent()):"");
        params.put("ip",reportDTO.getIp());
        params.put("os","android");
        params.put("time",reportDTO.getTraceTime());

        String callbackUrl=String.format(accountDTO.getDspCallbackUrl(),reportDTO.getBizRequestId());

        String urlParams= URLUtil.buildQuery(params, StandardCharsets.UTF_8)+"&callbackUrl="+ URLEncoderDecoderTool.encodeURL(callbackUrl);
        if(reportDTO.getEventType().equals("click")){
            return accountDTO.getDspReportClickUrl()+"&"+urlParams;
        }else{
            return accountDTO.getDspReportImpUrl()+"&"+urlParams;
        }

    }
}
